package com.gitee.cirnochat.common.controller;

import com.gitee.cirnochat.common.event.EmailSendEvent;
import com.gitee.cirnochat.common.model.vo.req.EmailSendRequest;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "邮件验证码接口", description = "邮件验证码相关API")
@RestController
@RequestMapping("/email")
@RequiredArgsConstructor
public class EmailCodeController {

    private final ApplicationEventPublisher eventPublisher;

     @Operation(summary = "发送邮箱验证码", description = "发送邮箱验证码")
     @ApiResponse(
             responseCode = "200",
             description = "成功",
             content = @Content(
                     mediaType = "application/json",
                     schema = @Schema(implementation = Result.class)
             )
     )
     @PostMapping("/send")
     public Result<Void> sendEmailCode(@Valid @RequestBody EmailSendRequest dto) {
         eventPublisher.publishEvent(new EmailSendEvent(this, dto.getEmail(), dto.getType()));
         return Result.success();
     }
}
